package com.taoyibao.hongcanting.ui.login.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.constant.CodeConstant;

import java.io.InputStream;

/**
 * @author TangAnna
 * @description: 引导页的Fragment
 * @date :${DATA} 17:18
 */
public class WelcomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_welcome, null);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int imgId = getArguments().getInt(CodeConstant.KEY);
        ImageView imageView = view.findViewById(R.id.iv_bg);
        imageView.setImageBitmap(readBitMap(getContext(), imgId));
    }

    /**
     * @param imgId 图片资源
     */
    public static WelcomeFragment getInstance(int imgId) {
        WelcomeFragment fragment = new WelcomeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(CodeConstant.KEY, imgId);
        fragment.setArguments(bundle);
        return fragment;
    }

    public Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }
}
